/**
 * Labox有限公司项目InnovationServer
 * Labox版权所有 @copyright 2010
 * @javaclass Punish.java @author 丁宁 @time 2010-8-3
 */
package labox.innovation.gameserver.model;

import labox.innovation.gameserver.Engine;
import labox.innovation.gameserver.GameTimeController;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.base.Race;
import labox.innovation.gameserver.timer.Timer;
import labox.innovation.gameserver.util.Broadcast;

/**
 * 玩家惩罚处理模型
 * 
 */
public final class Punish {

	public static enum PunishLevel {
		/**
		 * 无惩罚
		 */
		NONE(""),

		/**
		 * 禁言惩罚
		 */
		CHAT("禁言"),

		/**
		 * 红名监狱惩罚
		 */
		JAIL("红名监狱"),

		/**
		 * GM放入的监狱惩罚
		 */
		GMJAIL("GM监狱"),
		/**
		 * 禁用角色
		 */
		CHAR("角色禁用"),

		/**
		 * 禁用账户
		 */
		ACC("账户禁言");

		private String punString;

		PunishLevel(String string) {
			punString = string;
		}

		public int value() {
			return ordinal();
		}

		public String string() {
			return punString;
		}
	}

	private PunishLevel _punishLevel = PunishLevel.NONE;

	private FuzePcInstance _player;

	private long _punishTimer;

	private Timer _punishTask;

	public Punish(FuzePcInstance player) {
		_player = player;
	}

	public FuzePcInstance getPlayer() {
		return _player;
	}

	/**
	 * 返回玩家的惩罚等级
	 * 
	 * @return
	 */
	public PunishLevel getPunishLevel() {
		return _punishLevel;
	}

	public void setPunishLevel(int level) {
		_punishLevel = PunishLevel.values()[level];
	}

	/**
	 * 玩家是否在监狱中
	 * 
	 * @return True if player is jailed
	 */
	public boolean isInJail() {
		return _punishLevel == PunishLevel.JAIL;
	}

	/**
	 * 玩家是否聊天禁言
	 */
	public boolean isChatBanned() {
		return _punishLevel == PunishLevel.CHAT;
	}

	/**
	 * 设置玩家的惩罚等级
	 * 
	 * @param state
	 *            惩罚等级
	 * @param delayInMinutes
	 *            0 - 时间
	 */
	public void setPunishLevel(PunishLevel state, int delayInMinutes) {
		long delayInMilliseconds = delayInMinutes * 60000L;
		switch (state) {
		case NONE: //删除惩罚
			switch (_punishLevel) {
			case CHAT://如果原来是禁言惩罚
				_punishLevel = state;
				stopPunishTask();
				_player.sendMessage("你的禁言已被解禁");
				break;
			case JAIL:
			case GMJAIL:
				_punishLevel = state;
				// Open a Html message to inform the player
				// NpcHtmlMessage htmlMsg = new NpcHtmlMessage(0);
				// String jailInfos = HtmCache.getInstance().getHtm(getHtmlPrefix(),"data/html/jail_out.htm");
				// if (jailInfos != null)
				// htmlMsg.setHtml(jailInfos);
				// else
				// htmlMsg.setHtml("<html><body>You are free for now, respect server rules!</body></html>");
				// sendPacket(htmlMsg);
				stopPunishTask();
				if (_player.getRace() == Race.HUMAN)
					_player.teleToLocation(35, 633, 857);
				else if (_player.getRace() == Race.ELF)
					_player.teleToLocation(32, 540, 920);
				break;
			}
			break;
		case CHAT: //聊天禁言
			// not allow player to escape jail using chat ban
			if (_punishLevel == PunishLevel.JAIL || _punishLevel == PunishLevel.GMJAIL)
				break;
			_punishLevel = state;
			_punishTimer = 0;
			// // Remove the task if any
			stopPunishTask();
			if (delayInMinutes > 0) {
				_punishTimer = delayInMilliseconds;
				// start the countdown
				_punishTask = new Timer(_punishTimer, false, new PunishTask());
				_punishTask.start();
				_player.sendMessage("你的已经被禁言  " + delayInMinutes + " 分钟.");
			} else
				_player.sendMessage("你已经被禁言了");
			break;
		case JAIL: //进入监狱
			_punishLevel = state;
			// Remove the task if any
			stopPunishTask();
			//善恶值放入的监狱
			_punishTask = new Timer(60000L, true, new KarmaPunishTask());
			_punishTask.start();
			Broadcast.announceToOnlinePlayers("玩家 " + _player.getName() + " 由于杀人过多，已被乾坤咒镇住内丹并丢入监狱接受神罚要等待 " + _player.getKarma() + " 分钟后才能解封.");

			// // Open a Html message to inform the player
			// NpcHtmlMessage htmlMsg = new NpcHtmlMessage(0);
			// String jailInfos = HtmCache.getInstance().getHtm(getHtmlPrefix(),
			// "data/html/jail_in.htm");
			// if (jailInfos != null)
			// htmlMsg.setHtml(jailInfos);
			// else
			// htmlMsg.setHtml("<html><body>You have been put in jail by an admin.</body></html>");
			// sendPacket(htmlMsg);
			// setInstanceId(0);
			// setIsIn7sDungeon(false);
			Engine.getInstance().addTask(new Runnable() {
				public void run() {
					_player.teleToLocation(59, 346, 340); // Jail
				}
			});
			break;
		case GMJAIL:
			_punishLevel = state;
			_punishTimer = 0;
			// Remove the task if any
			stopPunishTask();
			if (delayInMinutes > 0) {//GM 放入监狱
				_punishTimer = delayInMilliseconds;
				// start the countdown
				_punishTask = new Timer(delayInMilliseconds, false, new PunishTask());
				_punishTask.start();
				Broadcast.announceToOnlinePlayers(_player.getName() + "被GM投入监狱中要等待 " + delayInMinutes + " 分钟后才能出来.");
			}
			Engine.getInstance().addTask(new Runnable() {
				public void run() {
					_player.teleToLocation(59, 346, 340); // Jail
				}
			});
			break;
		case CHAR: // 禁用角色
			_player.setAccessLevel(-100);
			_player.logout(false);
			break;
		case ACC: // 禁用账户
			_player.setAccountAccesslevel(-100);
			_player.logout(false);
			break;
		default:
			_punishLevel = state;
			break;
		}
	}

	public long getPunishTimer() {
		return _punishTimer;
	}

	public void setPunishTimer(long time) {
		_punishTimer = time;
	}

	/**
	 * 玩家进入世界后进入惩罚状态的继续
	 */
	public void updatePunishState() {
		if (_punishLevel != PunishLevel.NONE) {

			if (_punishLevel == PunishLevel.JAIL) {
				// 如果惩罚时间存在,重启启动解除惩罚任务
				_punishTask = new Timer(60000L, true, new KarmaPunishTask());
				_punishTask.start();
				_player.sendMessage("你当前还有 " + _player.getKarma() + " 分钟可离开监狱");
			} else if (_punishLevel == PunishLevel.GMJAIL) {
				// 如果惩罚时间存在,重启启动解除惩罚任务
				if (_punishTimer > 0) {
					_punishTask = new Timer(_punishTimer, true, new PunishTask());
					_punishTask.start();
					_player.sendMessage("你当前还有 " + _punishTimer / 60000 + " 分钟可离开监狱.");
				}
			}

			if (getPunishLevel() == PunishLevel.JAIL) {
				// If player escaped, put him back in jail
				//				if (!isInsideZone(ZONE_JAIL)) {
				//					if (getRace() == Race.HUMAN)
				//						teleToLocation(35, 633, 857);
				//					else if (getRace() == Race.ELF)
				//						teleToLocation(32, 540, 920);
				//				}
			}
		}
	}

	/**
	 * 停止解除惩罚的任务
	 * 
	 * @param save
	 *            计算剩下的惩罚时间
	 */
	public void stopPunishTask() {
		if (_punishTask != null) {
			if (_punishLevel == PunishLevel.GMJAIL)//如果GM放入的监狱，需要算出剩下的惩罚时间
				_punishTimer = (_punishTask.getActiveTime() - GameTimeController.getGameTicks()) * GameTimeController.MILLIS_IN_TICK;
			_punishTask.cancel();
		}
	}

	/**
	 * 按时间的解除惩罚的任务
	 * 
	 * @author Administrator
	 * 
	 */
	private class PunishTask implements Runnable {

		public void run() {
			setPunishLevel(PunishLevel.NONE, 0);
		}
	}

	/**
	 * 按善恶值的解除惩罚的任务
	 * 
	 * @author Administrator
	 * 
	 */
	private class KarmaPunishTask implements Runnable {

		public void run() {
			if (_player.getKarma() > 0)
				_player.setKarma(_player.getKarma() - 1);
			else
				setPunishLevel(PunishLevel.NONE, 0);

		}
	}
}
